<template>
    <div>
        <div>
            <el-table :data="assists" border stripe size="small">
                <el-table-column align="center" type="selection" width="55"></el-table-column>
                <el-table-column align="center" prop="id" label="编号" fixed width="120"></el-table-column>
                <el-table-column align="center" prop="assistName" label="助理名称" width="120"></el-table-column>
                <el-table-column align="center" prop="enabled" label="是否启用" width="200">
                    <template slot-scope="scope" style="align-items: center">
                        <el-tag size="small" type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag size="small" type="danger" v-else>未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="createDate" label="创建时间" width="120"></el-table-column>
                <el-table-column label="助理套餐" align="center">
                    <template slot-scope="scope">
                        <el-tooltip placement="right" v-if="scope.row.salary">
                            <div slot="content">
                                <table>
                                    <tr>
                                        <td>担任职责</td>
                                        <td>{{scope.row.salary.duty}}</td>
                                    </tr>
                                    <tr>
                                        <td>午餐补助</td>
                                        <td>{{scope.row.salary.launch}}</td>
                                    </tr>
                                    <tr>
                                        <td>奖金</td>
                                        <td>{{scope.row.salary.reward}}</td>
                                    </tr>
                                </table>
                            </div>
                            <el-tag>{{scope.row.salary.duty}}</el-tag>
                        </el-tooltip>
                        <el-tag v-else>暂未设置</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-popover
                                placement="left"
                                title="修改助理套餐"
                                @show="showPop(scope.row.salary)"
                                @hide="hidePop(scope.row)"
                                width="200"
                                trigger="click">
                            <div>
                                <el-select v-model="currentSalary" placeholder="请选择" size="mini">
                                    <el-option
                                            v-for="item in salaries"
                                            :key="item.id"
                                            :label="item.duty"
                                            :value="item.id">
                                    </el-option>
                                </el-select>
                            </div>
                            <el-button slot="reference" type="danger">修改助理套餐</el-button>
                        </el-popover>
                    </template>
                </el-table-column>
            </el-table>
            <div style="display: flex;justify-content: flex-end">
                <el-pagination
                        background
                        @size-change="sizeChange"
                        @current-change="currentChange"
                        layout="sizes, prev, pager, next, jumper, ->, total, slot"
                        :total="total">
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                assists: [],
                total: 0,
                currentPage: 1,
                currentSize: 10,
                currentSalary: null,
                salaries: []
            }
        },
        mounted() {
            this.initAssists();
            this.initSalaries();
        },
        methods: {
            sizeChange(size) {
                this.currentSize = size;
                this.initAssists();
            },
            currentChange(page) {
                this.currentPage = page;
                this.initAssists();
            },
            hidePop(data) {
                if (this.currentSalary) {
                    this.putRequest('/studentaffair/assist/?aid=' + data.id + '&sid=' + this.currentSalary).then(resp => {
                        if (resp) {
                            this.initAssists()
                        }
                    });
                }
            },
            showPop(data) {
                if (data) {
                    this.currentSalary = data.id;
                } else {
                    this.currentSalary = null;
                }
            },
            initSalaries() {
                this.getRequest("/system/salary/").then(res => {
                    if (res) {
                        this.salaries = res;
                    }
                })
            },
            initAssists() {
                this.getRequest("/studentaffair/assist/?page=" + this.currentPage + '&size=' + this.currentSize).then(res => {
                    if (res) {
                        this.assists = res.data;
                        this.total = res.total;
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>
